import { Base } from '../../utils/base.js';

class Model extends Base {
  constructor() {
    super();
  }

  // 提交报备
  setCustomer(data,callback) {
    this.request({
      type:'POST',
      url: '/set_customer',
      data:data,
      success: function (res) {
        callback && callback(res)
      },
      fail:function(res){
        callback && callback(res)
      }
    })
  }
  // 获取报备信息
  getCustomer(callback){
    this.request({
      url: '/get_customer',
      success: function (res) {
        callback && callback(res)
      },
      fail: function (res) {
        callback && callback(res)
      }
    })
  }

  // 上传图片
  setUpload(data,callback){
    var  formData= { img_type: data.index, id: data.customer_id }
    wx.uploadFile({
      url: this.baseRestUrl + '/customer_img',
      filePath: data.filePath + '',
      name: 'file',
      header: { 'content-type': 'application/json', 'token': wx.getStorageSync('token') },
      formData: formData,
      success: function (res) {
        var data = JSON.parse(res.data)
        var statusCode = res.statusCode;
        if (statusCode == 401) {   //token 错误
          var token = new Token();
          token.getToken((res) => {
            that.upload(customer_id, callback);
          })
        }
        wx.hideLoading();
        if (statusCode != 200) {  //上传失败
          wx.showToast({
            title: data.msg, icon: 'none'
          })
          return;
        }
        // 上传成功回调
        callback&&callback(res)
      },
      fail: function (res) {
        wx.showToast({
          title: '网络错误，请稍后重试~',
          icon: 'none'
        });
      }
    })
  }

  // 获取未通过审核的提交信息
  getCustomerInfo(id,callback){
    this.request({
      url: '/get_customer_info/'+id,
      success: function (res) {
        callback && callback(res)
      },
      fail: function (res) {
        callback && callback(res)
      }
    })  
  }
  
}

export { Model }